<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章关键字" prop="articleKeywords">
        <el-input
          v-model="queryParams.articleKeywords"
          placeholder="请输入文章关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:microcircle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:microcircle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:microcircle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:microcircle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="microcircleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="微圈id" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="文章图片" align="center" prop="articleUrls" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.articleUrls" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="isShow" :formatter="formatisShow">
      </el-table-column>
      <el-table-column label="序号" align="center" prop="serialNumber" />
      <el-table-column label="状态" align="center" prop="details">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.details === 2"
          >已通过</el-button>
          <div v-if="scope.row.details === 3">
            <el-button
            size="mini"
            type="danger"
            >退回</el-button>
          </div>
          <div v-if="scope.row.details === 1">
            <el-button
              size="mini"
              type="primary"
              @click="handleChangeDetails(scope.row, 2)"
            >通过</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleChangeDetails2(scope.row, 3)"
            >不通过</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="文章收藏量统计" align="center" prop="collectionNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSelect(scope.row)"
            v-hasPermi="['system:microcircle:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:microcircle:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微圈对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="文章关键字" prop="articleKeywords">
          <el-input v-model="form.articleKeywords" placeholder="请输入文章关键字" />
        </el-form-item>
        <el-form-item label="文章图片路径(多个逗号分隔)" prop="articleUrls">
          <image-upload v-model="form.articleUrls"/>
        </el-form-item>
        <el-form-item label="发布时间" prop="releaseTime">
          <el-date-picker clearable
            v-model="form.releaseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否显示(2显示,3隐藏)" prop="isShow">
          <el-radio v-model="radio" label="2">显示</el-radio>
            <el-radio v-model="radio" label="3">隐藏</el-radio>
        </el-form-item>
        <el-form-item label="序号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="文章内容">
          <editor v-model="form.articleContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="状态" prop="details">
          <el-input v-model="form.details" placeholder="请输入状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看微圈对话框 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="1000px" append-to-body custom-class="view-dialog">
      <el-descriptions class="margin-top" :column="3" :size="size" border>
          <el-descriptions-item label="用户id">{{ form.userId }}</el-descriptions-item>
          <el-descriptions-item label="文章关键字">{{ form.articleKeywords }}</el-descriptions-item>
          <el-descriptions-item label="发布时间">{{ form.releaseTime }}</el-descriptions-item>
          <el-descriptions-item label="序号">{{ form.serialNumber }}</el-descriptions-item>
          <el-descriptions-item label="是否显示">
            <el-radio v-model="form.isShow" label="2" disabled>显示</el-radio>
            <el-radio v-model="form.isShow" label="3" disabled>隐藏</el-radio>
          </el-descriptions-item>
          <el-descriptions-item label="状态">{{ form.details }}</el-descriptions-item>
          <el-descriptions-item label="文章点击数量">{{ form.articleHits }}</el-descriptions-item>
          <el-descriptions-item label="文章点赞数量">{{ form.articleLike }}</el-descriptions-item>
          <el-descriptions-item label="文章评论数量">{{ form.articleReviews }}</el-descriptions-item>
          <el-descriptions-item label="文章分享数量">{{ form.articleShare }}</el-descriptions-item>
          <el-descriptions-item label="文章收藏量统计">{{ form.collectionNum }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions class="margin-top" :column="2" :size="size" border>
          <el-descriptions-item label="文章图片路径">
            <image-preview :src="form.articleUrls" :width="200" :height="200"/>
          </el-descriptions-item>
          <el-descriptions-item label="文章内容">
            <editor v-model="form.articleContent" :min-height="192" disabled/>
          </el-descriptions-item>
      </el-descriptions>
        <div slot="footer" class="dialog-footer">
          <el-button @click="viewOpen = false">关 闭</el-button>
        </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMicrocircle, getMicrocircle, delMicrocircle, addMicrocircle, updateMicrocircle } from "@/api/siyun/microcircle";
import { getUserProfile } from "@/api/system/user"

export default {
  name: "Microcircle",
  data() {
    return {
      feedbackOpen: false, // 控制反馈对话框的显示与隐藏
      feedbackTitle: "", // 反馈对话框标题
      feedbackForm: {
        projectId: null,
        itemId: null,
        state: null,
        remarks: null,
        userId: null
      },
      feedbackRules: {
        // 规则就是用来打破的
      },
      //查看页面
      viewOpen: false,
      // 单选框
      radio: 2,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微圈表格数据
      microcircleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      userProfile: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        articleKeywords: null,
        articleUrls: null,
        createDate: null,
        releaseTime: null,
        articleHits: null,
        articleLike: null,
        articleReviews: null,
        articleShare: null,
        isShow: null,
        serialNumber: null,
        articleContent: null,
        updateUser: null,
        updateDate: null,
        details: null,
        collectionNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getUser();
  },
  methods: {
    handleChangeDetails(row, newStatus) {
      this.reset();
      updateMicrocircle({ id: row.id, details: newStatus })
        .then(response => {
          this.$message.success("状态更新成功");
          this.getList(); // 刷新列表以反映最新状态
        })
        .catch(error => {
          this.$message.error("状态更新失败");
        });
    },
    handleChangeDetails2(row, newStatus) {
      this.reset();
      this.feedbackOpen = true;
      this.feedbackForm.itemId = row.id; // 设置 itemId 为当前行的 id
      updateMicrocircle({ id: row.id, details: newStatus })
        .then(response => {
          this.$message.success("状态更新成功");
          this.getList(); // 刷新列表以反映最新状态
        })
        .catch(error => {
          this.$message.error("状态更新失败");
        });
    },
    handleSelect(row){
       this.reset();
          const id = row.id;
          getMicrocircle(id).then(response => {
            this.form = response.data;
            this.viewOpen = true;
            this.title = "查看微圈";
          });
    },
    formatisShow(row) {
      const statusMap = {
        2: '显示',
        3: '隐藏',
      };
      return statusMap[row.isShow] || '未知';
    },
    /** 查询微圈列表 */
    getList() {
      this.loading = true;
      listMicrocircle(this.queryParams).then(response => {
        this.microcircleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getUser() {
      getUserProfile().then(response => {
        console.log(response);
        this.userProfile = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        articleKeywords: null,
        articleUrls: null,
        createDate: null,
        releaseTime: null,
        articleHits: null,
        articleLike: null,
        articleReviews: null,
        articleShare: null,
        isShow: null,
        serialNumber: null,
        articleContent: null,
        updateUser: null,
        updateDate: null,
        details: null,
        collectionNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微圈";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMicrocircle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微圈";
      });
    },
    /** 提交按钮 */
    submitFeedback() {
      this.$refs["feedbackForm"].validate(valid => {
        if (valid) {
          this.feedbackForm.projectId = 1;
          this.feedbackForm.state = 3;
          this.feedbackForm.userId = this.userProfile.userId;
          this.feedbackForm.time = this.getCurrentTime();
          addFeedback(this.feedbackForm).then(response => {
            this.$message.success("反馈提交成功");
            this.feedbackOpen = false;
            this.getList(); // 刷新列表以反映最新状态
          }).catch(error => {
            this.$message.error("反馈提交失败");
          });
        }
      });
    },
    getCurrentTime() {
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      const hours = String(now.getHours()).padStart(2, '0');
      const minutes = String(now.getMinutes()).padStart(2, '0');
      const seconds = String(now.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    cancelFeedback() {
      this.resetFeedbackForm();
      this.feedbackOpen = false;
    },
    resetFeedbackForm() {
      this.feedbackForm = {
        projectId: null,
        itemId: null,
        state: null,
        remarks: null,
        userId: null
      };
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微圈编号为"' + ids + '"的数据项？').then(function() {
        return delMicrocircle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/microcircle/export', {
        ...this.queryParams
      }, `microcircle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
